/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pehulja.mmdo.coursework.gui;

import com.pehulja.mmdo.coursework.math.GoalFunction;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author pehulja
 */
public class DirectFunctionTableModel extends AbstractTableModel{
    private static final int defaultColumnCount = 3;
    private static final int defaultRowCount = 1;
    private Vector<Object> data;
    private Vector <String> columnIdentifiers;    
    public DirectFunctionTableModel() {
       this(defaultColumnCount);
    }

    public DirectFunctionTableModel(int varCount) {
       columnIdentifiers = new Vector<String>();
        for(int i = 0; i < varCount; i++)
            columnIdentifiers.add("x"+(i+1));
        columnIdentifiers.add("Extremum");
        data = new Vector<Object>();
        for(int i=0; i < varCount; i++){
                data.add("");
        }
        data.add(GoalFunction.Extremum.Max);
    }
    
    public int getRowCount() {
        return defaultRowCount;
    }

    public int getColumnCount() {
        return columnIdentifiers.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        return data.get(columnIndex);
    }

    @Override
    public String getColumnName(int column) {
        return (String) columnIdentifiers.get(column);
    }
    
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true; //To change body of generated methods, choose Tools | Templates.
    }

    public void setValueAt(Object value, int row, int col) {
        data.set(col, value);
        fireTableCellUpdated(row, col);
    }

    public void addColumnToEnd() {
        columnIdentifiers.insertElementAt("x"+(getColumnCount()), getColumnCount()-1); 
	data.insertElementAt("", data.size()-1);
        fireTableStructureChanged();
    }
    public void removePreLastColumn() throws Exception{
        if(getColumnCount()>=1){
            columnIdentifiers.remove(getColumnCount()-2); 
            data.remove(getColumnCount()-1);
            fireTableStructureChanged();
        }else{
            throw new Exception("Невозможно удалить столбец");
        }
    }
    
    public void setDataVector(Vector <Object> data){
        columnIdentifiers = new Vector<String>();
        for(int i = 0; i < data.size()-1; i++)
            columnIdentifiers.add("x"+(i+1));
        columnIdentifiers.add("Extremum");
        this.data = data;
        fireTableStructureChanged();
    }
   
}
